#include <pthread.h>
#include "list_pthread.h"

typedef DListLockRet (* LockFunc)(DListLock *thiz);
typedef	DListLockRet (* UnlockFunc)(DListLock *thiz);
typedef void ( *DestroyFunc)(DListLock *thiz);

enum {
    RET_OK,
    RET_FAIL
};


#if 0
struct d_list_lock {
    LockFunc    lock;
    UnLockFunc	unlock;
    DestroyFunc destroy; 
    char priv[0];
};
#endif

typedef struct priv_info {
    pthread_mutex_t mutex;
}PrivInfo;
static DListLockRet pthread_lock(DListLock *thiz);
static DListLockRet pthread_unlock(DListLock *thiz);
static void pthread_destroy(DListLock *thiz);
 
DListLock *DListLockCreate(void)
{
    DListLock* thiz = (DListLock *)malloc(sizeof(DListLock) + sizeof(PrivInfo));
    if (NULL != thiz){
	PrivInfo* priv = (PrivInfo *)thiz->priv;
	thiz->lock 	= pthread_lock;
	thiz->unlock 	= pthread_unlock;
	thiz->destroy	= pthread_destroy;
	pthread_mutex_init(&(priv->mutex), NULL);
    }

    return thiz;
}

static DListLockRet pthread_lock(DListLock *thiz)
{
    PrivInfo *priv = (PrivInfo *)thiz->priv;
    int ret = pthread_mutex_lock(&priv->mutex);
    return ret = 0 ? RET_OK : RET_FAIL;
}

static DListLockRet pthread_unlock(DListLock *thiz)
{
    PrivInfo *priv = (PrivInfo *)thiz->priv;
    int ret = pthread_mutex_unlock(&priv->mutex);
    return ret = 0 ? RET_OK : RET_FAIL;
}

static void pthread_destroy(DListLock *thiz)
{
    PrivInfo *priv = (PrivInfo *)thiz->priv;
    int ret = pthread_mutex_destroy(&priv->mutex);
    return;
}
    
